package com.project.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.project.bean.CoachBean;
import com.project.bean.CommentBean;
import com.project.bean.FolloweBean;
import com.project.bean.GymBean;
import com.project.bean.PageBean;
import com.project.bean.PostBean;
import com.project.bean.StudentBean;
import com.project.bean.UserBean;
import com.project.service.ICoachService;
import com.project.service.IFollowService;
import com.project.service.IGymService;
import com.project.service.IPostService;
import com.project.service.IStudentService;
import com.project.service.IUserService;
import com.project.util.GetCurrentTime;
import com.project.util.UploadImgUtil;
@Controller	
public class PostContholler {
		@Autowired 
	private IPostService postService;
		@Autowired
	private IFollowService followservice;
		@Autowired
	private IUserService userservice;
		@Autowired
	private IStudentService studentService;
		@Autowired
	private ICoachService coachService;
		@Autowired
	private IGymService gymService;
		
	//显示我的动态
	@GetMapping("/poster")
	public String showPost(int id,int page,int size,ModelMap map,HttpSession session) {
		int type=(int) session.getAttribute("type");
	PageBean pageBean=postService.findUserPostsByPage(id, page, size);
	//找到推荐场馆
	List<UserBean> list1=userservice.findUsersByType(3);
	//找到推荐教练
	List<UserBean> list2=userservice.findUsersByType(2);		

	PostBean post=new PostBean();
	post.setpDate("");
	post.setpMessage("");
	post.setpUId(id);
	post.setpImg("");
	List<CommentBean> commentslist=new ArrayList<CommentBean>();
	post.setCommentBeans(commentslist);
	List<Object> lists=new ArrayList<Object>();
	lists.add(post);
	if(pageBean.getList().isEmpty()) {
		pageBean.setList(lists);
	}
		List<FolloweBean> list=followservice.findFollowsById(id);
		List<FolloweBean> listfan=followservice.findFansById(id);
		UserBean user=userservice.findUserById(id);
		System.out.println(id);
		if(type==1) {
			StudentBean student=studentService.findStudentByUserId(id);
			System.out.println(student);
			if(student.getsMoney()==null) {
				student.setsMoney(0.0);
			}
			map.addAttribute("student", student);
		}else if(type==2) {
			CoachBean coach = coachService.findCoachById(id);
			if(coach.getcMoney()==null) {
				coach.setcMoney(0.0);
			}
			map.addAttribute("coach",coach);
		}else {
			GymBean gym = gymService.findGymById(id);
			map.addAttribute("gym", gym);
		}
		
		map.addAttribute("lists", list);
		map.addAttribute("listfan",listfan);
		map.addAttribute("pageBean", pageBean);
		map.addAttribute("user", user);
		map.addAttribute("Gyms", list1);
		map.addAttribute("Coachs", list2);

		if(type==1) {
			return "student_idex_self.html";
		}else if(type==2) {
			return "coach_idex_self.html";
		}else{
			return "gym_idex_self.html";
		}
		

		
	

	}
		//发送动态并显示
	@PostMapping("/postImgs")
	public String addPost(String Message,HttpServletRequest req,MultipartFile imgName,ModelMap map) {	
		    HttpSession session=req.getSession(false);
		    Object obj=session.getAttribute("UId");
		    int uId=(int) obj;
			PostBean post=new PostBean();
			String pictureName=UploadImgUtil.uploadimg(req, imgName); 
			post.setpImg(pictureName);
			post.setpUId(uId);
			post.setpMessage(Message);
            Date date=new Date();
			post.setpTime(date);
			postService.addPost(post);		         
		    return "redirect:/poster?id="+uId+"&page=1&size=4";
	}
					
	@GetMapping("/poster/version")
	@ResponseBody
    public PageBean showFriendsPost(int page,int size,HttpServletRequest req) {
		HttpSession session=req.getSession(false);	
		int uId=(int) session.getAttribute("UId");		
		List<FolloweBean> followList=followservice.findFansById(uId);
		if(!followList.isEmpty()) {
    	PageBean pageBean=postService.findAllFollowPosts(uId, page, size); 
    	System.out.println("pageBean是:"+pageBean);
		return pageBean; 
		}else {
		return new PageBean(); 
		}
    }
	  //删除动态
			@GetMapping("/deleteImgs")
		    public String deletePost(int id,HttpSession session){
			  int uId=(int) session.getAttribute("UId");
			  postService.deletePost(id);
			  return "redirect:/poster?id="+uId+"&page=1&size=4";
		}
}
